<?php
class Image{
    /**
     * [缩放操作]
     * @param $img [要缩放的图片]
     * @param $w [要缩放的宽]
     * @param $h [要缩放的高]
     */
    public function thumb($img,$w,$h){
        //目标图(画布)********
        $dstImg = imagecreatetruecolor($w,$h);
        //源图(大图)**********
        //获得图片类型
        $info = getimagesize($img);
        $type = ltrim(strrchr($info['mime'],'/'),'/');
        //使用变量函数打开不同类型的图片
        $fn = "imagecreatefrom{$type}";
        $srcImg = $fn($img);
        //($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
        imagecopyresized($dstImg,$srcImg,0,0,0,0,$w,$h,$info[0],$info[1]);
        //保存缩略图名称设置
        $fileName = "thumb_" . basename($img);
        $fullPath = dirname($img) . '/' . $fileName;
        //保存缩略图
        $fn = "image{$type}";
        $fn($dstImg,$fullPath);
        //销毁资源
        imagedestroy($dstImg);
        imagedestroy($srcImg);
        //返回完整路径
        return $fullPath;
    }

    /**
     * [水印操作]
     * @param $img [被加盖水印的图片]
     * @param $logo [水印图片]
     */
    public function water($img,$logo){
        //目标图(大图)**************
        $dstInfo = getimagesize($img);
        $dstType = $dstInfo['mime'];
        $dstType = strrchr($dstType,'/');
        $dstType = ltrim($dstType,'/');
        $fn = "imagecreatefrom{$dstType}";
        $dstImg = $fn($img);

        //源图(小图)****************
        $srcInfo = getimagesize($logo);
        $srcType = $srcInfo['mime'];
        $srcType = strrchr($srcType,'/');
        $srcType = ltrim($srcType,'/');
        $fn = "imagecreatefrom{$srcType}";
        $srcImg = $fn($logo);

        //($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct)
        $dstX = $dstInfo[0] - $srcInfo[0] - 10;
        $dstY = $dstInfo[1] - $srcInfo[1] - 10;
        imagecopymerge($dstImg,$srcImg,$dstX,$dstY,0,0,$srcInfo[0],$srcInfo[1],80);
        //保存图片
        $fn = "image{$dstType}";
        $fullPath = dirname($img) . "/water_" . basename($img);
        $fn($dstImg,$fullPath);
        //销毁
        imagedestroy($dstImg);
        imagedestroy($srcImg);
        //返回完整路径
        return $fullPath;
    }
}
$i = new Image();
//加完水印再缩略
//水印动作
$waterPath = $i->water('./shanghai.jpg','./logo.png');
//缩略动作
$path = $i->thumb($waterPath,200,100);
